﻿#coding = 'utf8'

import re
import os
from xml.etree import ElementTree
import cookielib, urllib2,urllib,base64
cj = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#pw = raw_input()
values = {'userName':'chenyh09', 'password':base64.b64decode('XXX=')}
data = urllib.urlencode(values)
url = 'https://academic.tsinghua.edu.cn:443/Login'
req = urllib2.Request(url,data)
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
r = opener.open(req)
ac = r.read()
#print ac
r.close()

tmp = ''
for x in cj:
  tmp=tmp+x.name+'='+x.value+'; '
tmp = tmp[0:-2]
print tmp
  


plogin = re.compile('(http:.+zhjw.+j_acegi_login.do\?.+?)"')
plearn = re.compile('(http:.+learn.+roam_newinfo.jsp\?.+?)"')

res = plearn.findall(ac)
for x in res:
  print x
  req = urllib2.Request(x)
  r = opener.open(req)
  print r.read()
  r.close()

res = plogin.findall(ac)
for x in res:
  x = x.replace('&amp;', '&')
  print x
  req = urllib2.Request(x)
  r = opener.open(req)
  print r.read()
  r.close()
  
#schedule
url = 'http://zhjw.cic.tsinghua.edu.cn/jxmh.do?m=bks_jxrl_all'
print cj
req = urllib2.Request(url)
#req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
r = opener.open(req)
print r.read()
r.close()

url = 'http://zhjw.cic.tsinghua.edu.cn/syxk.vsyxkKcapb.do?m=ztkbSearch&p_xnxq=2011-2012-1&pathContent=%D5%FB%CC%E5%BF%CE%B1%ED'
req = urllib2.Request(url)
r = opener.open(req)
print r.read()
r.close()

#get xls
values = {'m':'exportXLSZtkb','p_xnxq':'2011-2012-1','tokenPriFlag':'','selectKc':''}
data = urllib.urlencode(values)
req = urllib2.Request('http://zhjw.cic.tsinghua.edu.cn/syxk.vsyxkKcapb.do', data)
r = opener.open(req)
fd=open('out.xls','wb')
while(True):
  data=r.read(1024)
  if not len(data):
    break
  fd.write(data)
fd.close()
r.close()
